package com.rsxy.service.tools;


import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class JwtUtil {

    public String getToken(){
        String token = Jwts.builder()
                .claim("authorities", "guidada") // 配置用户角色
                .setSubject("测试...") // 配置主题
                .setExpiration(new Date(System.currentTimeMillis() + 60 *60 * 1000)) // 配置过期时间
                .signWith(SignatureAlgorithm.HS512, "MDk4ZjZiY2Q0NjIxZDM3M2NhZGU0ZTgzMjYyN2I0ZjY=") // 配置加密算法和密钥
                .claim("role","admin")
                .compact();
        return token;
    }
    public Claims getClaimsFromToken(String token) {
        Claims claims = Jwts.parser()
                .setSigningKey("MDk4ZjZiY2Q0NjIxZDM3M2NhZGU0ZTgzMjYyN2I0ZjY=")
                .parseClaimsJws(token)
                .getBody();
        return claims;
    }
    public  Boolean isTokenExpired(String token) {
        Date expiration = getExpirationDateFromToken(token);
        return expiration.before(new Date());
    }
    public Date getExpirationDateFromToken(String token) {
        Date expiration = getClaimsFromToken(token).getExpiration();
        return expiration;
    }
}
